import router from '@/router'
import pinia from './stores/index'
import { useUserStore } from '@/stores/modules/user.js'
import { computed } from 'vue'

const userStore = useUserStore(pinia)
const whiteRoute = []

// 添加动态路由
userStore.addRoutes()

router.beforeEach(async (to, from, next) => {
  document.title = window.config.systemName + '-' + to.name
  if (!to?.matched?.[to?.matched?.length - 1]?.components) {
    next('/404?path=' + to.path)
  }
  const token = computed(() => userStore.token)
  if (whiteRoute.includes(to.path)) {
    next()
  } else {
    if (token.value) {
      if (to.path === '/Login') {
        next(from.path)
      } else {
        next()
      }
    } else {
      if (to.path === '/Login') {
        next()
      } else {
        next('/Login')
      }
    }
  }
})
